clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * post

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

*create variables to control for financial crisis
sort zipcode year

gen cash_percent = cash_val_tran/sum_val_tran
gen ln_cash_percent = log(cash_percent)
gen tmp = ln_cash_percent if year == 2001
by zipcode: egen ln_cash_percent_01 = max(tmp)
drop tmp

gen foreshare = foreclosure/ct
gen lnforeshare = log(foreshare)
gen tmp = lnforeshare if year == 2001
by zipcode: egen lnforeshare_01 = max(tmp)
drop tmp

gen tmp = emp_fin2 if year == 2001
by zipcode: egen emp_fin2_01 = max(tmp)
drop tmp

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

*emp 
ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0 & (year < 2008 | year > 2011), absorb(timecounty) cluster(zipcode) 
eststo emp1
estadd local c "Yes"
estadd local ac ""
estadd local cyfe "Yes"
estadd local period "2012-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0 & (year < 2008 | year > 2011), absorb(timecounty) cluster(zipcode) 
eststo emp2
estadd local c "Yes"
estadd local ac ""
estadd local cyfe "Yes"
estadd local period "2012-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 lnforeshare (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp3
estadd local c "Yes"
estadd local ac "Foreclosure"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 lnforeshare (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp4
estadd local c "Yes"
estadd local ac "Foreclosure"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 ln_cash_percent (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp5
estadd local c "Yes"
estadd local ac "All-cash Transactions"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 ln_cash_percent (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp6
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "All-cash Transactions"
estadd local cyfe "Yes"
estadd local period "2008-2013"

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 emp_fin2 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp7
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "Financial Sector"
estadd local cyfe "Yes"
estadd local period "2008-2013"

ivreghdfe lnemp lnpopulation d_lnemp_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 emp_fin2 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo emp8
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "Financial Sector"
estadd local cyfe "Yes"
estadd local period "2008-2013"

esttab emp1 emp2 emp3 emp4 emp5 emp6 emp7 emp8 ///
	using "../results/table6a.tex", replace ///
	label ///
	keep(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	order(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c space ac space cyfe period fstat N , ///
		fmt(0 0 0 0 0 0 %3.0f %11.0g ) ///
		labels("Standard Controls" ///
			" " ///
			"Additional Controls" ///
			" " ///
			"Post Period" ///
			"County-year FE" ///
			"First Stage F-statistic" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) 
	



*zillow
ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0 & (year < 2008 | year > 2011), absorb(timecounty) cluster(zipcode) 
eststo z1
estadd local c "Yes"
estadd local ac ""
estadd local cyfe "Yes"
estadd local period "2012-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0 & (year < 2008 | year > 2011), absorb(timecounty) cluster(zipcode) 
eststo z2
estadd local c "Yes"
estadd local ac ""
estadd local cyfe "Yes"
estadd local period "2012-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 lnforeshare (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z3
estadd local c "Yes"
estadd local ac "Foreclosure"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 lnforeshare (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z4
estadd local c "Yes"
estadd local ac "Foreclosure"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 ln_cash_percent (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z5
estadd local c "Yes"
estadd local ac "All-cash Transactions"
estadd local cyfe "Yes"
estadd local period "2008-2013"
estadd scalar fstat = e(widstat)

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 ln_cash_percent (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z6
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "All-cash Transactions"
estadd local cyfe "Yes"
estadd local period "2008-2013"

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 emp_fin2 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z7
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "Financial Sector"
estadd local cyfe "Yes"
estadd local period "2008-2013"

ivreghdfe lnzhvi_real lnpopulation d_lnzhvi_real_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 emp_fin2 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chinese100cash!= 0, absorb(timecounty) cluster(zipcode) 
eststo z8
estadd scalar fstat = e(widstat)
estadd local c "Yes"
estadd local ac "Financial Sector"
estadd local cyfe "Yes"
estadd local period "2008-2013"


esttab z1 z2 z3 z4 z5 z6 z7 z8 ///
	using "../results/table6b.tex", replace ///
	label ///
	keep(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	order(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(c space ac space cyfe period fstat N , ///
		fmt(0 0 0 0 0 0 %3.0f %11.0g ) ///
		labels("Standard Controls" ///
			" " ///
			"Additional Controls" ///
			" " ///
			"County-year FE" ///
			"Post Period" ///
			"First Stage F-statistic" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) 
